//
//  LJHFavoriteViewController.m
//  HEARME
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHFavoriteViewController.h"
#import "LJHFavorTableViewCell.h"
#import "LJHAlbumsDetailViewController.h"
#import "LJHContentTableViewCell.h"
#import "LJHAlbumListTableViewCell.h"
@interface LJHFavoriteViewController ()<UITableViewDataSource, UITableViewDelegate,refresh>

@property(nonatomic,retain)LJHBaseTableView *favorTableView;
@property(nonatomic,retain)NSMutableArray *recommendArray;
@property(nonatomic,retain)NSMutableArray *myAlbumArray;


@end

@implementation LJHFavoriteViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.myAlbumArray = [NSMutableArray arrayWithArray:[[LJHFMData shareFMDB] selectAlbumData]];
    [self.favorTableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
    [GiFHUD show];
    
    // Do any additional setup after loading the view.
    self.recommendArray = [NSMutableArray array];
    [self createTableView];
}
#pragma mark - 数据请求
-(void)getData
{
    
    
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/album/recommend/list/unlogin?device=iphone";
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        //        NSLog(@"%@", result);
        NSMutableArray *dataArray = [result objectForKey:@"frDatas"];
        //        NSLog(@"%@", dataArray);
        
        for (NSMutableDictionary *dic in dataArray) {
            
            LJHAlbumModel *model = [LJHAlbumModel modelWithDictionary:dic];
            
            model.isFavor = NO;
            
            NSString *strTracks = [dic objectForKey:@"albumPlay"];
            
            model.tracksCounts = strTracks;
            
            model.albumCoverUrl290 = [dic objectForKey:@"coverSmall"];
            
            model.myId = [dic objectForKey:@"albumId"];
            
            if ([[[LJHFMData shareFMDB] selectAlbumIdData] containsObject:model.myId]) {
                
                model.isFavor = YES;
                
            }
            
            [self.recommendArray addObject:model];
            
        }
        
        [self.favorTableView reloadData];
        
        [self.favorTableView.header endRefreshing];
        
    } error:^(NSError *error) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
}

#pragma mark - 创建tableView
-(void)createTableView
{
    
    self.favorTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-111) style:UITableViewStyleGrouped];
    
    [self.view addSubview:self.favorTableView];
    
    [_favorTableView release];
    
    self.favorTableView.dataSource = self;
    
    self.favorTableView.delegate = self;
    
    [self.favorTableView registerClass:[LJHFavorTableViewCell class] forCellReuseIdentifier:@"favorTableCell"];
    
    [self.favorTableView registerClass:[LJHContentTableViewCell class] forCellReuseIdentifier:@"con"];
    
    [self.favorTableView reloadData];
    
    self.favorTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    
    self.favorTableView.header = header;
    
    [self.favorTableView.header beginRefreshing];
    
}

#pragma mark 设置tableview区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return self.myAlbumArray.count;
        
    }
    else
    {
        
        return self.recommendArray.count;
        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJHFavorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"favorTableCell"];
    
    if (indexPath.section == 0) {
        
        LJHChildListModel *model = [self.myAlbumArray objectAtIndex:indexPath.row];
        cell.model = model;
        cell.nextBtn.tag = [model.albumId integerValue];
        [cell.nextBtn setImage:[UIImage imageNamed:@"iconfont-icon52-2"] forState:UIControlStateNormal];
        [cell.nextBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    if (indexPath.section == 1) {
        
         LJHContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"con"];
        LJHAlbumModel *model = [self.recommendArray objectAtIndex:indexPath.row];
        cell.albumModel = model;
        cell.nextBtn.alpha = 0;
//        LJHAlbumListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryTableCell"];
//        
//        cell.albumModel = (LJHAlbumModel *)[self.recommendArray objectAtIndex:indexPath.row];
        
//        return cell;
//        [cell.nextBtn setImage:[UIImage imageNamed:@"find_album_unfav_n"] forState:UIControlStateNormal];
//        [cell.nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.nextBtn.tag = indexPath.row;
        cell.myrefresh = self;
        return cell;
        
    }
    
    
    return cell;
}

-(void)refresh
{
    [self.favorTableView reloadData];
}

#pragma mark - 删除按钮方法
-(void)deleteAction:(UIButton *)btn
{
    UIAlertController *alterC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认要删除此专辑?" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alterC animated:YES completion:^{
        
        
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[LJHFMData shareFMDB] deleteByAlbumId:[NSString stringWithFormat:@"%ld", btn.tag]];
        self.myAlbumArray = [NSMutableArray arrayWithArray:[[LJHFMData shareFMDB] selectAlbumData]];
        [self.favorTableView reloadData];
    }];
    
    UIAlertAction *channelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alterC addAction:channelAction];
    [alterC addAction:sureAction];
}
-(void)nextAction:(UIButton *)btn
{
    LJHAlbumModel *model = [self.recommendArray objectAtIndex:btn.tag];
    
    if (model.isFavor) {
        [[LJHFMData shareFMDB] deleteByAlbumId:model.myId];
        [btn setImage:[UIImage imageNamed:@"find_album_unfav_n@2x.png"] forState:UIControlStateNormal];
        model.isFavor = NO;
    }
    else{
        [[LJHFMData shareFMDB] insertAlbumData:model];
        [btn setImage:[UIImage imageNamed:@"find_album_fav_n@2x.png"] forState:UIControlStateNormal];
        model.isFavor = YES;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH*0.3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return @"推荐收藏";
    }
    return @"我收藏的专辑";
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        LJHAlbumsDetailViewController *albumsDetailVC = [[LJHAlbumsDetailViewController alloc] init];
        [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        
        albumsDetailVC.albumId = ((LJHChildListModel *)self.myAlbumArray[indexPath.row]).albumId;
        //        albumsDetailVC.myTitle = ((TOChildListModel *)self.myAlbumArray[indexPath.row]).albumTitle;
//        albumsDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    else if (indexPath.section == 1){
        LJHAlbumsDetailViewController *albumsDetailVC = [[LJHAlbumsDetailViewController alloc] init];
        [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        
        albumsDetailVC.albumId = ((LJHAlbumModel *)self.recommendArray[indexPath.row]).myId;
        albumsDetailVC.myTitle = ((LJHAlbumModel *)self.recommendArray[indexPath.row]).albumTitle;
//        albumsDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
}
- (void)statusBarTouchedAction {
    
    self.favorTableView.scrollsToTop = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
